﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Model;
using Dapper.Contrib.Extensions;
using System.Data;
using Weight.Base;
using Dapper;
using Weight.Model.ParamEntity;

namespace Weight.Dal.Contract
{
   public   class DalContractRecharge
    {
        /// <summary>
        /// 添加充值信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(ContractTicketRechargeEntity entity)
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                conn.Insert(entity);
                return true;
            }
        }

        /// <summary>
        /// 查询充值记录
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public IEnumerable<ContractTicketRechargeEntity> List(QueryContractChargeReq req)
        {
            string sql = "select * from ContractTicketRecharge where TicketId=@TicketId";
            if( !string.IsNullOrEmpty( req.ChargeUser))
            {
                req.ChargeUser = "%" + req.ChargeUser + "%";
                sql += " and Recharger like @ChargeUser";
            }
            if(!string.IsNullOrEmpty(req.EndDate) && !string.IsNullOrEmpty(req.StartDate))
            {
                sql += " and Convert(DateTime,RechargeTime)>=Convert(DateTime,@StartDate)";
                sql += " and Convert(DateTime,RechargeTime)<=Convert(DateTime,@EndDate)";
            }
            sql += " order by   Convert(DateTime,RechargeTime) desc ";
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                return conn.Query<ContractTicketRechargeEntity>(sql, req);
            }
        }
    }
}
